package com.tonann.app.mqtt.mulitipleServer;

import com.tonann.app.domain.AppGateway;
import com.tonann.app.domain.AppSensor;
import com.tonann.app.mqtt.dao.MQTTSensor;
import com.tonann.app.service.IAppGatewayService;
import com.tonann.app.service.IAppSensorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 项目启动 监听主题
 */
@Slf4j
//@Component
public class MqttClientListener {//  implements ApplicationListener<ContextRefreshedEvent> {

//    private volatile AtomicBoolean isInit = new AtomicBoolean(false);
//
//    @Autowired
//    private IAppGatewayService gatewayService;
//
//    @Autowired
//    private IAppSensorService sensorService;
//
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
//        try {
//            //防止重复触发
//            if (!isInit.compareAndSet(false, true)) {
//                return;
//            }
//            AppGateway appGateway = new AppGateway();
//            appGateway.setGatewayType("2");
//            appGateway.setStatus("0");
//            List<AppGateway> gatewayList = gatewayService.selectAppGatewayList(appGateway);
////            List<AppGateway> gatewayList = new ArrayList<>();
////            AppGateway gate = new AppGateway();
////            gate.setIpAddress("127.0.0.1");
////            gate.setPort("1883");
////            gatewayList.add(gate);
//            List<String> uris = new ArrayList<>();
//            for (AppGateway gateway : gatewayList) {
//                String url = "tcp://" + gateway.getIpAddress() + ":" + gateway.getPort();
//                MqttClientConnect mqttClientConnect = new MqttClientConnect();
//                mqttClientConnect.setMqttClientId("mqtt" + UUID.randomUUID());
//                String host = "tcp://" + gateway.getIpAddress() + ":" + gateway.getPort();
//                mqttClientConnect.setMqttClient(host, mqttClientConnect.getMqttClientId(), null, null, false, new MqttClientCallback(mqttClientConnect.getMqttClientId()));
//                mqttClientConnect.sub(StringUtils.commaDelimitedListToStringArray("#"));
//                MqttClientConnect.mqttClients.put(mqttClientConnect.getMqttClientId(), mqttClientConnect);
//                MqttClientConnect.mqttBrokers.put(gateway.getIpAddress() + ":" + gateway.getPort(), mqttClientConnect);
//                log.info("{}--连接成功！！订阅主题--{}", mqttClientConnect.getMqttClientId(), "#");
//            }
//            List<AppSensor> appSensorList = sensorService.selectAppSensorListByGatewayType("2");
//            for (AppSensor sensor : appSensorList) {
//                String tag = sensor.getMqttDeviceChannel() + "." + sensor.getMqttDeviceName() + "." + sensor.getMqttDeviceTag();
//                MQTTSensor.getInstance().map.put(tag, sensor);
//            }
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//        }
//    }

}
